<?php

/*
	This file is part of Mandragon.

    Mandragon is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Mandragon is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Mandragon.  If not, see <http://www.gnu.org/licenses/>.
*/

require_once 'PHPUnit/Framework.php';
require_once 'autoload.php';

class TestFiche extends PHPUnit_Framework_TestCase {
	function test_get_edit_everything() {
		$expected_id = 321;
		$expected_sql = 'sql';
		$expected_sql_result = 'sql result';
		$expected_result = 'result';

		$page = new Page();
		$event = new Fiche($page, $expected_id);

		$mock_db_access = $this->getMock('DbAccessor');
		$mock_db_access->expects($this->once())
				->method('db_query_select')
				->with(array(array('*')), array("GAME_FICHE"), "item_id = $expected_id")
				->will($this->returnValue($expected_sql));
		$mock_db_access->expects($this->once())
				->method('db_do_query')
				->with($expected_sql)
				->will($this->returnValue($expected_sql_result));
		$mock_db_access->expects($this->once())
				->method('db_fetch_array')
				->with($expected_sql_result)
				->will($this->returnValue($expected_result));
		$event->set_db_accessor($mock_db_access);

		$actual = $event->get_edit_everything();

		$this->assertEquals($expected_result, $actual);
	}

	function test_check_content() {
		$expected_game_id = 321;
		$expected_game_type = 'game type';
		$expected_game_name = 'game name';
		$expected_game_editor_id = 432;
		$expected_languages = 'languages';
		$expected_price = 'price';
		$expected_did_you_know = 'did you know';
		$expected_expansions = 'expansions';
		$expected_requirements = 'requirements';
		$expected_rules = 'rules';
		$expected_players = 'players';
		$expected_genre = 'genre';
		$expected_description = 'description';
		$expected_values = array('game_id' => $expected_game_id,
				'game_type' => $expected_game_type,
				'game_name' => $expected_game_name,
				'game_editor_id' => $expected_game_editor_id,
				'description' => $expected_description,
				'genre' => $expected_genre,
				'players' => $expected_players,
				'rules' => $expected_rules,
				'requirements' => $expected_requirements,
				'expansions' => $expected_expansions,
				'did_you_know' => $expected_did_you_know,
				'price' => $expected_price,
				'languages' => $expected_languages);
		$expeced_new = false; 

		$page = new Page();
		$fiche = new Fiche($page);

		$mock_checker = $this->getMock('InputChecker');
		$mock_checker->expects($this->at(0))
				->method('check_int')
				->with($expected_game_id, "spel")
				->will($this->returnValue(true));
		$mock_checker->expects($this->at(1))
				->method('check_input')
				->with($expected_game_id > 0, "spel niet ingevuld")
				->will($this->returnValue(true));
		$mock_checker->expects($this->at(2))
				->method('check_int')
				->with($expected_game_editor_id, "uitgever")
				->will($this->returnValue(true));
		$mock_checker->expects($this->at(3))
				->method('check_input')
				->with($expected_game_editor_id > 0, "uitgever niet ingevuld")
				->will($this->returnValue(true));
		$mock_checker->expects($this->at(4))
				->method('check_tags_advanced')
				->with($expected_description, "Beschrijving") 
				->will($this->returnValue(true));
		$mock_checker->expects($this->at(5))
				->method('check_tag_closures')
				->with($expected_description)
				->will($this->returnValue(true));
		$mock_checker->expects($this->at(6))
				->method('check_exists')
				->with($expected_description, "Beschrijving")
				->will($this->returnValue(true));
		$mock_checker->expects($this->at(7))
				->method('check_tags_advanced')
				->with($expected_genre, "Genre")
				->will($this->returnValue(true));
		$mock_checker->expects($this->at(8))
				->method('check_tag_closures')
				->with($expected_genre)
				->will($this->returnValue(true));
		$mock_checker->expects($this->at(9))
				->method('check_exists')
				->with($expected_genre, "Genre")
				->will($this->returnValue(true));
		$mock_checker->expects($this->at(10))
				->method('check_tags_advanced')
				->with($expected_players, "Spelers")
				->will($this->returnValue(true));
		$mock_checker->expects($this->at(11))
				->method('check_tag_closures')
				->with($expected_players)
				->will($this->returnValue(true));
		$mock_checker->expects($this->at(12))
				->method('check_exists')
				->with($expected_players, "Spelers")
				->will($this->returnValue(true));
		$mock_checker->expects($this->at(13))
				->method('check_tags_advanced')
				->with($expected_rules, "Regels")
				->will($this->returnValue(true));
		$mock_checker->expects($this->at(14))
				->method('check_tag_closures')
				->with($expected_rules)
				->will($this->returnValue(true));
		$mock_checker->expects($this->at(15))
				->method('check_exists')
				->with($expected_rules, "Regels")
				->will($this->returnValue(true));
		$mock_checker->expects($this->at(16))
				->method('check_tags_advanced')
				->with($expected_requirements, "Benodigdheden")
				->will($this->returnValue(true));
		$mock_checker->expects($this->at(17))
				->method('check_tag_closures')
				->with($expected_requirements)
				->will($this->returnValue(true));
		$mock_checker->expects($this->at(18))
				->method('check_tags_advanced')
				->with($expected_expansions, "Uitbreidingen")
				->will($this->returnValue(true));
		$mock_checker->expects($this->at(19))
				->method('check_tag_closures')
				->with($expected_expansions)
				->will($this->returnValue(true));
		$mock_checker->expects($this->at(20))
				->method('check_tags_advanced')
				->with($expected_did_you_know, "Weetjes")
				->will($this->returnValue(true));
		$mock_checker->expects($this->at(21))
				->method('check_tag_closures')
				->with($expected_did_you_know)
				->will($this->returnValue(true));
		$mock_checker->expects($this->at(22))
				->method('check_tags_advanced')
				->with($expected_price, "Richtprijs")
				->will($this->returnValue(true));
		$mock_checker->expects($this->at(23))
				->method('check_tag_closures')
				->with($expected_price)
				->will($this->returnValue(true));
		$mock_checker->expects($this->at(24))
				->method('check_exists')
				->with($expected_price, "Richtprijs")
				->will($this->returnValue(true));
		$mock_checker->expects($this->at(25))
				->method('check_tags_advanced')
				->with($expected_languages, "Taal")
				->will($this->returnValue(true));
		$mock_checker->expects($this->at(26))
				->method('check_tag_closures')
				->with($expected_languages)
				->will($this->returnValue(true));
		$mock_checker->expects($this->at(27))
				->method('check_exists')
				->with($expected_languages, "Taal")
				->will($this->returnValue(true));
		$fiche->set_input_checker($mock_checker);

		$actual = $fiche->check_content($expected_values, $expected_new);
		$this->assertTrue($actual);
	}

	function test_check_content_with_new_game() {
		$expected_game_id = 'NEW';
		$expected_game_type = 'game type';
		$expected_game_name = 'game name';
		$expected_game_editor_id = 432;
		$expected_languages = 'languages';
		$expected_price = 'price';
		$expected_did_you_know = 'did you know';
		$expected_expansions = 'expansions';
		$expected_requirements = 'requirements';
		$expected_rules = 'rules';
		$expected_players = 'players';
		$expected_genre = 'genre';
		$expected_description = 'description';
		$expected_values = array('game_id' => $expected_game_id,
				'game_type' => $expected_game_type,
				'game_name' => $expected_game_name,
				'game_editor_id' => $expected_game_editor_id,
				'description' => $expected_description,
				'genre' => $expected_genre,
				'players' => $expected_players,
				'rules' => $expected_rules,
				'requirements' => $expected_requirements,
				'expansions' => $expected_expansions,
				'did_you_know' => $expected_did_you_know,
				'price' => $expected_price,
				'languages' => $expected_languages);
		$expeced_new = false; 

		$page = new Page();
		$fiche = new Fiche($page);

		$mock_checker = $this->getMock('InputChecker');
		$mock_checker->expects($this->at(0))
				->method('check_int')
				->with($expected_game_type, "spel - soort")
				->will($this->returnValue(true));
		$mock_checker->expects($this->at(1))
				->method('check_input')
				->with($expected_game_type, "spel - soort niet ingevuld")
				->will($this->returnValue(true));
		$mock_checker->expects($this->at(2))
				->method('check_exists')
				->with($expected_game_name, "spel - naam")
				->will($this->returnValue(true));
		$mock_checker->expects($this->at(3))
				->method('check_tags')
				->with($expected_game_name, "spel - naam")
				->will($this->returnValue(true));
		$mock_checker->expects($this->at(4))
				->method('check_int')
				->with($expected_game_editor_id, "uitgever")
				->will($this->returnValue(true));
		$mock_checker->expects($this->at(5))
				->method('check_input')
				->with($expected_game_editor_id > 0, "uitgever niet ingevuld")
				->will($this->returnValue(true));
		$mock_checker->expects($this->at(6))
				->method('check_tags_advanced')
				->with($expected_description, "Beschrijving") 
				->will($this->returnValue(true));
		$mock_checker->expects($this->at(7))
				->method('check_tag_closures')
				->with($expected_description)
				->will($this->returnValue(true));
		$mock_checker->expects($this->at(8))
				->method('check_exists')
				->with($expected_description, "Beschrijving")
				->will($this->returnValue(true));
		$mock_checker->expects($this->at(9))
				->method('check_tags_advanced')
				->with($expected_genre, "Genre")
				->will($this->returnValue(true));
		$mock_checker->expects($this->at(10))
				->method('check_tag_closures')
				->with($expected_genre)
				->will($this->returnValue(true));
		$mock_checker->expects($this->at(11))
				->method('check_exists')
				->with($expected_genre, "Genre")
				->will($this->returnValue(true));
		$mock_checker->expects($this->at(12))
				->method('check_tags_advanced')
				->with($expected_players, "Spelers")
				->will($this->returnValue(true));
		$mock_checker->expects($this->at(13))
				->method('check_tag_closures')
				->with($expected_players)
				->will($this->returnValue(true));
		$mock_checker->expects($this->at(14))
				->method('check_exists')
				->with($expected_players, "Spelers")
				->will($this->returnValue(true));
		$mock_checker->expects($this->at(15))
				->method('check_tags_advanced')
				->with($expected_rules, "Regels")
				->will($this->returnValue(true));
		$mock_checker->expects($this->at(16))
				->method('check_tag_closures')
				->with($expected_rules)
				->will($this->returnValue(true));
		$mock_checker->expects($this->at(17))
				->method('check_exists')
				->with($expected_rules, "Regels")
				->will($this->returnValue(true));
		$mock_checker->expects($this->at(18))
				->method('check_tags_advanced')
				->with($expected_requirements, "Benodigdheden")
				->will($this->returnValue(true));
		$mock_checker->expects($this->at(19))
				->method('check_tag_closures')
				->with($expected_requirements)
				->will($this->returnValue(true));
		$mock_checker->expects($this->at(20))
				->method('check_tags_advanced')
				->with($expected_expansions, "Uitbreidingen")
				->will($this->returnValue(true));
		$mock_checker->expects($this->at(21))
				->method('check_tag_closures')
				->with($expected_expansions)
				->will($this->returnValue(true));
		$mock_checker->expects($this->at(22))
				->method('check_tags_advanced')
				->with($expected_did_you_know, "Weetjes")
				->will($this->returnValue(true));
		$mock_checker->expects($this->at(23))
				->method('check_tag_closures')
				->with($expected_did_you_know)
				->will($this->returnValue(true));
		$mock_checker->expects($this->at(24))
				->method('check_tags_advanced')
				->with($expected_price, "Richtprijs")
				->will($this->returnValue(true));
		$mock_checker->expects($this->at(25))
				->method('check_tag_closures')
				->with($expected_price)
				->will($this->returnValue(true));
		$mock_checker->expects($this->at(26))
				->method('check_exists')
				->with($expected_price, "Richtprijs")
				->will($this->returnValue(true));
		$mock_checker->expects($this->at(27))
				->method('check_tags_advanced')
				->with($expected_languages, "Taal")
				->will($this->returnValue(true));
		$mock_checker->expects($this->at(28))
				->method('check_tag_closures')
				->with($expected_languages)
				->will($this->returnValue(true));
		$mock_checker->expects($this->at(29))
				->method('check_exists')
				->with($expected_languages, "Taal")
				->will($this->returnValue(true));
		$fiche->set_input_checker($mock_checker);

		$actual = $fiche->check_content($expected_values, $expected_new);
		$this->assertTrue($actual);
	}
}

?>
